/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package searchspads.util;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.border.Border;
import javax.swing.plaf.BorderUIResource;
import searchspads.ContactEditor;

public class JComboCheckBox extends JComboBox {
 
    private int maxWidth = 300;
    public ContactEditor contact;
    public JComboCheckBox() {
        super();
        setRenderer(new ComboCheckBoxRenderer());
        updateUI();
    }
 
    public JComboCheckBox(String[] items,ContactEditor c) {
        super();
        contact = c;
        setRenderer(new ComboCheckBoxRenderer());
        addItems(items);
        updateUI();
    }
 
    public JComboCheckBox(Vector<String> items,ContactEditor c) {
        super();
        contact = c;
        setRenderer(new ComboCheckBoxRenderer());
        addItems(items.toArray(new String[0]));
        updateUI();
    }
 
    public JComboCheckBox(int maxWidth) {
        super();
        this.maxWidth = maxWidth;
        setRenderer(new ComboCheckBoxRenderer());
        updateUI();
    }
 
    public JComboCheckBox(String[] items, int maxWidth) {
        super();
        this.maxWidth = maxWidth;
        setRenderer(new ComboCheckBoxRenderer());
        addItems(items);
        updateUI();
    }
 
    public JComboCheckBox(Vector<String> items, int maxWidth) {
        super();
        this.maxWidth = maxWidth;
        setRenderer(new ComboCheckBoxRenderer());
        addItems(items.toArray(new String[0]));
        updateUI();
    }
 
    public void addItems(String[] items) {
        //this.addItem("aaaa");
        for (int i = 0; i < items.length; i++) {
            String string = items[i];
            this.addItem(new ComboCheckBoxEntry(String.valueOf(i + 1), string));
        }
    }
    public void addItem(String str){
        super.addItem(str);
    }
    public void addItem(ComboCheckBoxEntry item) {
        super.addItem(item);
    }
 
    public void addItem(boolean checked, boolean state, String id, String value) {
        super.addItem(new ComboCheckBoxEntry(checked, state, id, value));
    }
 
    public String[] getCheckedCodes() {
        Vector values = new Vector();
        DefaultComboBoxModel model = (DefaultComboBoxModel) getModel();
        for (int i = 0; i < model.getSize(); i++) {
            ComboCheckBoxEntry item = (ComboCheckBoxEntry) model.getElementAt(i);
            boolean checked = item.getChecked();
            if (checked) {
                values.add(item.getUniqueCode());
            }
        }
        String[] retVal = new String[values.size()];
        values.copyInto(retVal);
        return retVal;
    }
 
    public String[] getCheckedValues() {
        Vector values = new Vector();
        DefaultComboBoxModel model = (DefaultComboBoxModel) getModel();
        for (int i = 0; i < model.getSize(); i++) {
            ComboCheckBoxEntry item = (ComboCheckBoxEntry) model.getElementAt(i);
            boolean checked = item.getChecked();
            if (checked) {
                values.add(item.getValue());
            }
        }
        String[] retVal = new String[values.size()];
        values.copyInto(retVal);
        return retVal;
    }
 
    @Override
    public void updateUI() {
       setUI(new ComboCheckBoxUI(maxWidth,contact));
    }
 
//    public static void main(String[] args) {
//        JFrame frame = new JFrame();
//        frame.setSize(650, 580);
//        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 
//        JPanel jPanel = new JPanel();
//    Vector v = new Vector();
//      v.add("Europe");
//      v.add(new JCheckBox("Brussels", false));
//      v.add(new JCheckBox("Paris", false));
//      v.add(new JCheckBox("London", false));
//      v.add(new JCheckBox("United States",false));
//      v.add(new JCheckBox("New York", false));
//      v.add(new JCheckBox("Detroit", false));
//        String[] values = new String[]{"1", "22", "33333333333333333333", 
//"4444444444444", "55555555555555555", "6", "77", "6", "77", "6", "77"};
//        final JComboCheckBox checkBox = new JComboCheckBox(values);
//        checkBox.setBorder(javax.swing.BorderFactory.createLineBorder(Color.BLACK, 2));
//        checkBox.setPreferredSize(new Dimension(300, 30));
//        jPanel.add(checkBox);
//        
//        JButton btnCode = new JButton("Code");
//        btnCode.addActionListener(new ActionListener() {
// 
//            public void actionPerformed(ActionEvent e) {
//                for (String string : checkBox.getCheckedCodes()) {
//                    System.out.print(string + " ");
//                }
//                System.out.println("");
//            }
//        });
//        jPanel.add(btnCode);
// 
//        JButton btnValue = new JButton("Value");
//        btnValue.addActionListener(new ActionListener() {
// 
//            public void actionPerformed(ActionEvent e) {
//                for (String string : checkBox.getCheckedValues()) {
//                    System.out.print(string + " ");
//                }
//                System.out.println("");
//            }
//        });
//        jPanel.add(btnValue);
// 
//        frame.getContentPane().add(jPanel);
//        frame.setVisible(true);
//    }
    
    
//    class ComboBoxRenderer implements ListCellRenderer {
//      private JLabel label;
//       
//      public ComboBoxRenderer() {
//         setOpaque(true);
//      }
//       
//      public Component getListCellRendererComponent(JList list, Object value, int index,
//                                                    boolean isSelected, boolean cellHasFocus) {
//         if (value instanceof JCheckBox) {
//            list.repaint();
//         }
//         if (value instanceof Component) {
//            Component c = (Component)value;
//            if (isSelected) {
//               c.setBackground(list.getSelectionBackground());
//               c.setForeground(list.getSelectionForeground());
//            } else {
//               c.setBackground(list.getBackground());
//               c.setForeground(list.getForeground());
//            }
//             
//            return c;
//         } else {
//            if (label==null) {
//               label = new JLabel(value.toString());
//            }
//            else {
//               label.setText(value.toString());
//            }
//             
//            return label;
//         }
//      }
//   }
}